<template>
  <div class="my-container">
<!-- 用户信息(已登录) -->
    <div class="header user-info" v-if='store.state.user'>
      <div class="base-info">
        <div class="left">
          <van-image :src="userInfo.data.photo" class="avatar" round fit='cover'/>
          <span class="name">{{userInfo.data.name}}</span>
        </div>
        <div class="right">
          <van-button size='mini' round to="/user/profile">编辑资料</van-button>
        </div>
      </div>
      <div class="data-stats">
          <div class="data-item"><span class="count">{{userInfo.data.art_count}}</span><span class="text">头条</span></div>
          <div class="data-item"><span class="count">{{userInfo.data.follow_count}}</span><span class="text">关注</span></div>
          <div class="data-item"><span class="count">{{userInfo.data.fans_count}}</span><span class="text">粉丝</span></div>
          <div class="data-item"><span class="count">{{userInfo.data.like_count}}</span><span class="text">获赞</span></div>
      </div>
    </div>
    <!-- 登录/注册(未登录) -->
    <div class="header not-login" v-else>
      <div class="login-btn" @click="$router.push('/login')">
        <img src="@/assets/mobile.png" alt="" class="mobile-img">
        <span class="text">登录/注册</span>
      </div>
    </div>
    <!-- 收藏/历史 -->
    <van-grid :column-num="2" class="grid-nav" clickable>
      <van-grid-item class="grid-item"><template #icon><i class="toutiao toutiao-shoucang"></i></template><template #text><span class="text">收藏</span></template></van-grid-item>
      <van-grid-item class="grid-item"><template #icon><i class="toutiao toutiao-lishi"></i></template><template #text><span class="text">历史</span></template></van-grid-item>
    </van-grid>
    <!-- 消息通知/小智同学/退出登录 -->
    <van-cell-group>
      <van-cell title="消息通知" is-link/>
      <van-cell title="小智同学" is-link/>
    </van-cell-group>
    <van-cell title="退出登录" class="lgout-cell" v-if="store.state.user" @click="onLogout" clickable/>
  </div>
</template>

<script>
import { useStore } from 'vuex'
import { Dialog, Toast } from 'vant'
import { getUserInfo } from '@/api/user'
import { reactive } from 'vue'

export default {
  name: 'MyIndex',
  setup () {
    const store = useStore()// vuex自带响应式
    const userInfo = reactive({ data: {} })// 写入空属性让网页第一次加载时不报错
    if (store.state.user) {
      loadUserInfo()
    }
    async function loadUserInfo () {
      try {
        const { data } = await getUserInfo()
        // setup中函数里直接赋值会无效，因此赋值对象属性，但一定要先设定空的该属性才不报错
        userInfo.data = data.data
      } catch (err) {
        if (err.response && err.response.status === 401) {
          Toast.fail('请重新登录')
        } else {
          Toast.fail('获取失败')
        }
      }
    }
    function onLogout () {
      // 确认退出提示
      Dialog.confirm({
        title: '确认退出吗？'
      })
        .then(() => {
          // 确认
          store.commit('setUser', null)
        })
        .catch(() => {
          // 取消
        })
    }
    return {
      onLogout,
      userInfo,
      store
    }
  }
}
</script>

<style lang='less' scoped>
.my-container {
  .header {
    height: 361px;
    background:url('@/assets/banner.png');
    background-size: cover;//以图片不变形的方式100%充满容器
  }
  .not-login {
    display: flex;//居中前置
    justify-content: center;//水平居中
    align-items: center;//垂直居中
    .login-btn {
      display: flex;
      flex-direction: column;//垂直排布
      justify-content: center;
      align-items: center;
      .mobile-img {
      width: 132px;
      height: 132px;
      margin-bottom: 15px;
      }
      .text {
      font-size: 28px;
      color: #fff;
      }
    }
  }
  .user-info {
    .base-info {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;//这句话能让padding不撑大div
      display: flex;
      justify-content: space-between;//水平分散
      align-items: center;
      .left {
        display: flex;
        align-items: center;
        .avatar {
          width: 132px;
          height: 132px;
          margin-right: 23px;
          border: 4px solid #fff
        }
        .name {
          font-size: 30px;
          color: #fff;
        }
      }
    }
    .data-stats {
      display: flex;
      .data-item {
        flex:1;//使所有子组件水平平分父组件
        display: flex;
        flex-direction: column;//垂直排布
        justify-content: center;
        align-items: center;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
  }
  .grid-nav {
    margin-bottom: 8px;
    .grid-item {
      .toutiao {
        font-size: 45px;
      }
      .toutiao-shoucang {
          color: #eb5253
        }
      .toutiao-lishi {
          color:#ff9d1d
      }
      .text {
        font-size: 28px;
      }
    }
  }
  .lgout-cell{
    margin-top: 9px;
    text-align: center;
    color: #d86262;
  }
}
</style>
